#include <Windows.h>
#include <Windowsx.h>
#include <tchar.h>
#include "resource.h"

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0103"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);

			break;
		}
		case WM_CHAR:
		{
			break;
		}
		case WM_SYSKEYDOWN:
		{
			TCHAR szKeyName[128] = {0};
			GetKeyNameText(lParam, szKeyName, 128);

			TCHAR szTmp[MAX_PATH];
			_stprintf(szTmp, _T("Sys Key Value: 0x%x Name: %s"), wParam, szKeyName);
			ListBox_AddString(GetDlgItem(hDlg, IDC_LIST1), szTmp);
			SendMessage(GetDlgItem(hDlg, IDC_LIST1), WM_VSCROLL, SB_BOTTOM, 0);

			SetFocus(hDlg);

			break;
		}
		case WM_KEYDOWN:
		{
			TCHAR szKeyName[128] = {0};
			GetKeyNameText(lParam, szKeyName, 128);

			TCHAR szTmp[MAX_PATH];
			_stprintf(szTmp, _T("Key Value: 0x%x Name: %s"), wParam, szKeyName);
			ListBox_AddString(GetDlgItem(hDlg, IDC_LIST1), szTmp);
			SendMessage(GetDlgItem(hDlg, IDC_LIST1), WM_VSCROLL, SB_BOTTOM, 0);
			
			SetFocus(hDlg);

			break;
		}
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	HWND hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);
	if (NULL != hWnd)
	{
		ShowWindow(hWnd, SW_SHOW);
		UpdateWindow(hWnd);
		
		MSG Msg;
		while (GetMessage(&Msg, NULL, NULL, NULL))
		{
			if (Msg.message == WM_KEYDOWN || Msg.message == WM_SYSKEYDOWN)
			{
				SendMessage(hWnd, Msg.message, Msg.wParam, Msg.lParam);
			} 
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}

	//DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}